package com.ztorn.services.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztorn.dtos.MyasnHdrDTO;
import com.ztorn.mapper.AsnMapper;
import com.ztorn.model.MyasnHdr;
import com.ztorn.model.MypktTrackevent;
import com.ztorn.services.AsnService;
import com.ztorn.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AsnServiceImpl extends ServiceImpl<AsnMapper, MyasnHdr> implements AsnService {

    @Autowired
    private AsnMapper asnMapper;

    @Override
    public List<MyasnHdrDTO> selectMyasnHdrList(String orderNos, String co) {
        return asnMapper.selectMyasnHdrList(orderNos,co);
    }

    @Override
    @Transactional
    public void putInStorage(String orderNos, String location, String co, List<MyasnHdrDTO> myasnHdrDTOList) {
        // TODO Auto-generated method stub
        // 修改订单状态为30-已入库
        asnMapper.updateBatchState(orderNos, co);
        // 把订单信息批量插入库存表
        for (MyasnHdrDTO m: myasnHdrDTOList) {
            m.setLocation(location);
        }
        asnMapper.insertInventory(myasnHdrDTOList);
        // 需要生成日志的订单，自营仓订单不必生成
        List<MyasnHdrDTO> ostatList = new ArrayList<MyasnHdrDTO>();
        // 批量生成状态信息
        for (MyasnHdrDTO dto: myasnHdrDTOList) {
            if ("OFFLINE".equals(dto.getAsnShpmtType())) {
                dto.setExpFlag("T");
            } else {
                dto.setExpFlag("N");
            }
            ostatList.add(dto);
        }
        if (ostatList.size() > 0) {
            asnMapper.insertOstatExpLog(ostatList);
        }
        // 生成跟踪信息并批量插入
        List<MypktTrackevent> eventList = getEvent(myasnHdrDTOList);
        asnMapper.saveEvent(eventList);
    }

    public List<MypktTrackevent> getEvent(List<MyasnHdrDTO> myasnHdrDTOList) {
        List<MypktTrackevent> eventList = new ArrayList<MypktTrackevent>();
        for (MyasnHdrDTO dto: myasnHdrDTOList) {
            MypktTrackevent event = new MypktTrackevent();
            // 放天猫LP开头的物流单号
            event.setShpmtNbr(ToolUtil.isNotEmpty(dto.getMiscInstrCode1())? dto.getMiscInstrCode1(): dto.getAsnNbr());
            event.setPktCtrlNbr(ToolUtil.isEmpty(dto.getParentOrder())?"":dto.getParentOrder());
            event.setEventDate(new Date());
            event.setEventDesc("总包到达国际邮件处理中心(Bulk Package received at International Mail Processing Center)");
            event.setEventLoc("深圳 Shenzhen");
            event.setEventStat("IN");
            event.setWhse(dto.getWhse());
            event.setCo(dto.getCo());
            event.setDiv(dto.getDiv());
            event.setCreateDate(new Date());
            event.setExpFlag(10);
            event.setUserId("TM");
            if ("OFFLINE".equals(dto.getAsnShpmtType())) {
                event.setUpFlag("T");
            } else {
                event.setUpFlag("N");
            }
            eventList.add(event);
        }
        return eventList;
    }
}